import schedule from "node-schedule";
import fetch from "node-fetch";
import crypto from "crypto";

// 机器人地址
let baseUrl = `https://oapi.dingtalk.com/robot/send?access_token=1a011505f417ae5206283b94005b5ba4e0f6ff201dd25215d6f4713b92e315e3`;

// 签名验证
let secret =
  "SECc3a15e94ca03f880d6ef35c151d97080b2a58ed54eeeef621a7f35e7917a9a5d";

// 时间点和对应操作
const tasks = [
  {
    time: "09:00",
    title: "早上通知",
    text: "### 早上通知\n这是早上的通知内容。",
  },
  {
    time: "11:00",
    title: "上午通知",
    text: "### 上午通知\n这是上午的通知内容。",
  },
  {
    time: "11:50",
    title: "午餐通知",
    text: "### 午餐通知\n该吃午饭啦！",
  },
  {
    time: "15:00",
    title: "下午通知",
    text: "### 下午通知\n这是下午的通知内容。",
  },
  {
    time: "17:25",
    title: "下班通知",
    text: "### 下班通知\n准备下班啦！",
  },
];

// 发送钉钉消息函数
async function sendDingTalkMessage(title, text) {
  let timestamp = Date.now();
  let stringToSign = `${timestamp}\n${secret}`;
  let base = crypto
    .createHmac("sha256", secret)
    .update(stringToSign)
    .digest("base64");
  let sign = encodeURIComponent(base);
  let url = `${baseUrl}&timestamp=${timestamp}&sign=${sign}`;

  let body = {
    msgtype: "markdown",
    markdown: {
      title: title,
      text: text,
    },
    at: {
      atMobiles: [
        "12345678901", // 指定用户手机号
        "19876543210",
      ],
      isAtAll: false,
    },
  };

  try {
    let response = await fetch(url, {
      method: "post",
      body: JSON.stringify(body),
      headers: { "Content-Type": "application/json" },
    });
    let msg = await response.json();
    console.log(`消息发送成功: ${title}`, msg);
  } catch (error) {
    console.error(`消息发送失败: ${title}`, error);
  }
}

// 为每个时间点创建任务
tasks.forEach(({ time, title, text }) => {
  const [hour, minute] = time.split(":".map(Number));

  schedule.scheduleJob({ hour, minute }, () => {
    console.log(`执行任务: ${title} at ${time}`);
    sendDingTalkMessage(title, text);
  });
});

console.log("定时任务已启动。");

/*
let body = {
  msgtype: "markdown",
  markdown: {
    title: "通知标题", // 消息卡片的标题
    text:
      "### 通知标题\n" +
      "这是通知的内容。\n\n" +
      "![图片描述](https://example.com/path-to-your-image.jpg)\n\n" +
      "@12345678901 @19876543210", // 在文本中显式标注@用户
  },
  at: {
    atMobiles: [
      "12345678901", // 指定用户手机号
      "19876543210",
    ],
    isAtAll: false, // 是否@全体成员
  },
};

上班：https://k.sinaimg.cn/n/sinakd20240218s/480/w640h640/20240218/e5e0-c594275ab4fdbf4ee550056e5c2cb04a.jpg/w700d1q75cms.jpg

吃饭：https://qcloud.dpfile.com/pc/nzg9XysQ-kssY0R7uKifBLy7d_1f9koFuG-28CJSBdLCWFIIWqBhNqkuVvwMoGcdtmHC6yvA4vz9HlIwfUxnnbGzA5roSQdOIZBdpbY56uQFaWqA0j7ta3_A22czT6CvR2wBLHpNWleM8u_F-3h0csaq1-bq6jn4ci07g10pOcR8r-pAC7Z8Me1lmvv-C1rCtuD2Ttc33o2oiy9iGY5KAqS8eU1WRISyVZ5oaCuzdo9GQLj2BUn2Fvl5kTJ57OrBF71mYN0a8QgyjWdwktTu-cbzP0nagghY6N9-FW_oTwsY3M7cV0bPjsGcOFw_OelnA59_ReXiWFf_E_ZrP50hh0PsRG77oQfkN68xkMvdWvLkdLPtM7Qhu-5cOXcgfglxkzNIfpCrIG6E8pGjNbWE8nXbFwGNjZo9c9Z6Vy4CHQ7qtJlzaLV2abhdoP7WVQK3Ef4oG-QCuFbBACo7HPEn7Xr70gCcopIf4ISpYtgJUFc5koQbR7qtYnjgb4nKg60IngjvItjm2Pi9k9HGxctJL3iwTu_9PoM68-B3FwT_ZzXTtMzAm9Ao1OzMu6UzwwhDcnevFMP8gjC5vn_-sW8cW1E6ydPph68pn_IBwOnU5FwwXQKcVMr19MQGdAcvTpBH.jpg

下班：https://img0.baidu.com/it/u=3077512197,1041199722&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=561

休息 1：https://i0.hdslb.com/bfs/archive/62c544965572afc97615781367fba27380dcf99f.jpg

*/
